package com.dd.cloud.common.utils;

import org.springframework.core.convert.converter.Converter;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description: String对象转换为LocalTime对象  // 类说明，在创建类时要填写
 * @ClassName: LocalTimeConverter    // 类名，会自动填充
 * @Author: weisn          // 创建者
 * @Date: 2022/9/27 16:15   // 时间
 * @Version: 1.0     // 版本
 */
public class LocalTimeConverter implements Converter<String, LocalTime> {

    private final DateTimeFormatter formatter;

    public LocalTimeConverter(String dateFormat) {
        this.formatter = DateTimeFormatter.ofPattern(dateFormat);
    }

    @Override
    public LocalTime convert(String source) {
        if (source == null || source.isEmpty()) {
            return null;
        }

        return LocalTime.parse(source, formatter);
    }
}
